/**
 *  文件名:BaseActivity.java
 *  修改人:lihangjie
 *  创建时间:2015-8-29 下午10:57:10
 */
package com.sharemob.tinchat.base;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;

import com.sharemob.tinchat.R;
import com.sharemob.tinchat.thirdparty.weixin.WXConstant;

/**
 * 
 * <一句话功能简述>
 *
 * @author lihangjie
 * version [版本号,2015-8-29 下午10:57:10]
 * @see    [相关类/方法]
 * @since  [产品/模块版本]
 *
 */
public abstract class BaseActivity extends Activity {
	

	public void BtnEvent(int id,final Callback callback){
		   findViewById(id).setOnClickListener(new OnClickListener()
	        {
	            public void onClick(View v)
	            {
	            	callback.doDown();
	            }
	        });
	}
	protected abstract void keyEventOfListView(int position,int id);
	protected TextView tv_title=null;
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        
    }
    
    protected void setTitle(String title_name){
    	tv_title=(TextView)findViewById(R.id.tv_title);
    	tv_title.setText(title_name);
    }
    
//    protected ParallaxScollListView getInitListViewForPet(ArrayList<PetItem> list,int id,boolean listener){
//    	ParallaxScollListView listView=(ParallaxScollListView)findViewById(id);
//		ListAdapterForPet listAdapter=new ListAdapterForPet();
//		
//		listAdapter.setAdapter(list);
//		listView.setAdapter(listAdapter);
//	
//		if(listener){
//			listView.setOnItemClickListener(new OnItemClickListener(){
//	            public void onItemClick(AdapterView<?> parent, View view, int position,long id)
//	            {
//	            	keyEventOfListView(position,parent.getId());
//	            }
//	        });
//		}
//		
//		return listView;  
//	}

    
    protected NoScrollListview getInitListView(ArrayList<Item> list,int id,boolean listener){
		NoScrollListview listView=(NoScrollListview)findViewById(id);
		ListAdapter listAdapter=new ListAdapter();
		
		listAdapter.setAdapter(list);
		listView.setAdapter(listAdapter);
	
//		listAdapter.notifyDataSetChanged();
		if(listener){
			listView.setOnItemClickListener(new OnItemClickListener(){
	            public void onItemClick(AdapterView<?> parent, View view, int position,long id)
	            {
	            	keyEventOfListView(position,parent.getId());
	            }
	        });
		}
		
		return listView;  
	}

	protected String getItemFromMap(Map<String,String> map,int _index){
		Iterator iter = map.entrySet().iterator();
		String id=null;
		int index=0;
		while (iter.hasNext()) {  
            Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();  
            String key = entry.getKey();  
            String name = entry.getValue(); 
            if(index==_index){
            	id=key;
            	System.out.println(key + "__ " + name);  
            	break;
            }else{
            	index++;
            }
            
		}
		return id;	
	}
	
	protected NoScrollListview getInitListView(Map<String,String> map,int id){
		ArrayList<Item> list = new  ArrayList< Item>(); 
		NoScrollListview listView=(NoScrollListview)findViewById(id);
		ListAdapter listAdapter=new ListAdapter();
		
		Iterator iter = map.entrySet().iterator();
		while (iter.hasNext()) {  
            Map.Entry<String, String> entry = (Map.Entry<String, String>) iter.next();  
            String key = entry.getKey();  
            String name = entry.getValue();  
              
            String[] names=name.split("\\,");
             
            //添加item 属性
            Item item=new Item();
            item.setId(key);
            item.setIcon(false);
            
            if(names.length>1){
            	item.setName(names[0]); 
            	item.setRightName(names[1]);
            	System.out.println(names[0]+"---"+names[1]);
            }else{
            	item.setName(name); 
            }
            
            list.add(item);
        }  
		
		listAdapter.setAdapter(list);
		listView.setAdapter(listAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener(){
	            public void onItemClick(AdapterView<?> parent, View view, int position,long id)
	            {
	            	keyEventOfListView(position,parent.getId());
	            }
	        });
		return listView;  
	}
    protected void exit() {
        finish();
        overridePendingTransition(0,R.anim.activity_close);
        // android.os.Process.killProcess(android.os.Process.myPid());
    }
    
    public void finishActivity(){
    	finish();
    	//关闭窗体动画显示
        overridePendingTransition(0,R.anim.activity_close);
    }
    
    public void exitView(int id,final int animId,final int exitAnim){
        findViewById(id).setOnClickListener(new OnClickListener()
        {
            public void onClick(View v)
            { 
            	finish();
            	//关闭窗体动画显示
                overridePendingTransition(exitAnim,animId);
            }
        });
    }
    
    public void gotoView(Context ctx,Class clazz){
		Intent intent = new Intent(); 
        intent.setClass(ctx,clazz);
        startActivity(intent);
        overridePendingTransition(R.anim.activity_open,0);
	}
 
    public void gotoViewByAnim(Context ctx,Class clazz,int enterAnim,boolean isfinish){
  		  Intent intent = new Intent(); 
          intent.setClass(ctx,clazz);
          startActivity(intent);
          if(isfinish){
        	  finish();
          }
          overridePendingTransition(enterAnim,0);
  	}
    /** 
     * 显示Toast消息 
     * @param msg 
     */  
    protected void showToast(String msg){  
    	Toast mToast =Toast.makeText(this, msg, Toast.LENGTH_SHORT);  
        mToast.setText(msg);  
        mToast.show();  
    }  
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	  if (keyCode== KeyEvent.KEYCODE_BACK) {
    		  finishActivity();
              return  false;
          }
		return false;
    }
    

}
